<?php
class Load
{
	public static function config($filename, $is_sys_config = true, $app_name = '')
	{
		if ($is_sys_config) {
			$dir = SYS_CFG_DIR;
			$cache_pre = 'sys_';
		} else {
			$dir = CFG_DIR;
			$cache_pre = $app_name . '_';
		}
		if (is_array($filename)) {
			foreach($filename as $item) {
				self::config($item);
			}
		} else {
			$cache = SYS_CACHE_DIR . $cache_pre . $filename . '.php';
			$file = $dir . $filename . '.ini';
			self::cache($file, $cache_pre . $filename, $cache_pre . $cache);
		}
	}
	
	public static function lang($lang, $is_sys_lang = true, $app_name = '')
	{
		if ($is_sys_lang) {
			$dir = SYS_LANG_DIR;
			$cache_pre = 'sys_';
		} else {
			$dir = LANG_DIR;
			$cache_pre = $app_name . '_';
		}
		$file = $dir . $lang . '.ini';
		$cache = SYS_CACHE_DIR . $cache_pre . $lang . '.php';
		self::cache($file, $cache_pre . $lang, $cache_pre . $cache);
	}
	
	public static function cache($file, $filename, $cache)
	{
		if (file_exists($file)) {
			if (file_exists($cache)) {
				include_once $cache;
				if (isset($GLOBALS[$filename][$filename . '_mtime'])) {
					$fmtime = $GLOBALS[$filename][$filename . '_mtime'];
					if ($fmtime != filemtime($file)) {
						Cache::set($filename, parse_ini_file($file), filemtime($file));
						
					}
				}
			} else {
				Cache::set($filename, parse_ini_file($file), filemtime($file));
			}
		} else {
			die("Error: '$file' is not exist.");
		}
	}
	
	public static function read($fileName, $keyName)
	{
		$res = '';
		if (file_exists(SYS_CACHE_DIR . $fileName . '.php') && isset($GLOBALS[$fileName])) {
			$res = $GLOBALS[$fileName][$keyName];
		}
		return $res;
	}
	
	/*public static function mod($name = 'site', $params = array())
	{
		if (isset($GLOBALS['run']['router'])) {
			if ($GLOBALS['run']['router']) {
				$router = new Router();
				$name = $router->getParam(1);
			} else {
				if (isset($_GET[$GLOBALS['run']['modname']])) {
					$name = Unit::safe($_GET[$GLOBALS['run']['modname']]);
				}
			}
		} else {
			die('Config error!');
		}
		$file = MOD_DIR . strtolower($name) . '-mod.php';
		if (file_exists($file)) {
			include_once $file;
			if (class_exists($name)) {
				$mod = new $name;
				$mod->run();
			}
		}
	}*/
}